package com.zhida.smartcontrol.mvp.permisson;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import androidx.fragment.app.Fragment;

import com.jay.tools.base.BaseFragment;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;


/**
 * author: tanjianpei
 * date: 2018/5/8 008 13:45
 * <p>
 * description:
 */
@SuppressLint("CheckResult")
public abstract class PermissionCheckerFragment extends BaseFragment {

    protected void requestFilePermission() { // 子类申请权限
        final RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        openFile(aBoolean);
                    }
                });
    }

    protected void openFile(Boolean aBoolean) {
        // 子类实现具体方法
    }

    protected void requestCameraPermission() { // 子类申请权限
        final RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEachCombined(Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            openCamera();
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 拒绝一次，没有选中永不提醒
                            noPermission();
                        } else {
                            // 永久拒绝
                            noPermission();
                        }
                    }
                });
    }

    protected void openCamera() {
        // 子类实现具体方法
    }

    protected void requestContactsPermission() { // 子类申请权限
        final RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(
                Manifest.permission.READ_CONTACTS)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            readContacts();
                        } else if (permission.shouldShowRequestPermissionRationale) {

                        } else {
                            // 永久拒绝
                        }
                    }
                });
    }

    protected void readContacts() {
        // 子类实现具体方法
    }

    protected void requestLocation() { // 子类申请权限
        final RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(
                Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(new Consumer<Permission>() {

                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            location(true);
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            // 拒绝一次，没有选中永不提醒
                            location(false);
                        } else {
                            location(false);
                        }
                    }
                });
    }

    protected void location(boolean hasLocation) { // 申请权限成功
        // 子类实现具体方法
    }

    protected abstract void noPermission();

    /**
     * 显示权限申请提示框
     */
    protected void showTipsDialog(final Object cxt) {
        final Context context;
        if (cxt instanceof Activity) {
            context = (Activity) cxt;
        } else if (cxt instanceof Fragment) {
            context = ((Fragment) cxt).getContext();
        } else {
            throw new RuntimeException("cxt is not a activity or fragment");
        }

        new AlertDialog.Builder(context).setTitle("提示信息")
                .setMessage("当前应用缺少必要权限，要使用此功能，请单击【确定】按钮前往设置中心进行权限授权。")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                openAppSettings(cxt);
            }
        }).show();

    }

    /**
     * 启动当前应用设置页面
     */
    private void openAppSettings(Object cxt) {
        if (cxt instanceof Activity) {
            Activity activity = (Activity) cxt;
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + activity.getPackageName()));
            activity.startActivity(intent);
        } else if (cxt instanceof Fragment) {
            Fragment fragment = (Fragment) cxt;
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + fragment.getContext().getPackageName()));
            fragment.startActivity(intent);
        } else {
            throw new RuntimeException("cxt is net a activity or fragment");
        }
    }

}
